package org.jsola.contract.provider.impl;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jsola.common.StrKit;
import org.jsola.contract.provider.IUserProviderService;
import org.jsola.user.core.TokenUser;
import org.jsola.user.entity.ThirdInfoDO;
import org.jsola.user.entity.UnionOpenIdDO;
import org.jsola.user.service.IThirdInfoService;
import org.jsola.user.service.IUnionOpenIdService;
import org.jsola.user.service.IUserService;
import org.jsola.user.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author zxy
 */
@Slf4j
@Service("contractUserProviderService")
public class UserProviderServiceImpl implements IUserProviderService {

    @Autowired
    private IThirdInfoService thirdInfoService;

    @Autowired
    private IUnionOpenIdService unionOpenIdService;

    @Autowired
    private IUserService userService;


    @Override
    public UnionOpenIdDO getUnionOpenIdByUser(String useId, String appId) {
        ThirdInfoDO thirdInfoDO = thirdInfoService.selectByUserIdAndAppId(useId, appId);
        log.info("======第三方信息：{}", thirdInfoDO);
        if (Objects.isNull(thirdInfoDO)) {
            return null;
        }
        String unionId = thirdInfoDO.getUnionId();
        if (StringUtils.isEmpty(unionId)) {
            return null;
        }
        return unionOpenIdService.selectOpenId(unionId, appId);
    }

    @Override
    public UserVO selectById(String userId) {
        if (StrKit.isEmpty(userId)) {
            return null;
        }
        return userService.selectById(userId);
    }
}
